package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.annotation.BusinessLog;
import com.example.annotation.FlowControl;
import com.example.annotation.Idempotent;
import com.example.mapper.master.UserMapper;
import com.example.model.*;
import com.example.service.IdempotentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author long
 * @date 2022/11/30 15:12
 */
//@CrossOrigin(origins = "*"
//        ,allowedHeaders = "x-requested-with"
//        ,allowCredentials = "true"
//        ,maxAge = 3600
//        ,methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS,RequestMethod.DELETE})
@RestController
@RequestMapping(value = "/admin")
public class AdminController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private IdempotentService idempotentService;

	@CrossOrigin(origins = "*"
            ,allowedHeaders = "x-requested-with"
            ,allowCredentials = "true"
            ,maxAge = 3600
            ,methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS,RequestMethod.DELETE})
    @ResponseBody
    @GetMapping("/testGet")
    public String testGet(@RequestParam String param){
        System.out.println("入参: "+param);
        return "success";
    }

    @Idempotent
    @PostMapping("/testIdempotent")
    public String testIdempotent(@RequestBody GoodsDTO dto) {
        idempotentService.testIdempotent();
        return "success";
    }

    @FlowControl
    @PostMapping("/testFlowControl")
    public String testFlowControl(@RequestBody GoodsDTO dto) {
        idempotentService.testFlowControl();
        return "success";
    }

    @GetMapping("/testMvc")
    public String testMvc(@RequestParam("param") String param) {
        System.out.println("测试web-mvc执行流程");
        return "success";
    }

    @BusinessLog(functionDesc = "测试Mybatis的Ognl语法")
    @GetMapping("/testMybatisOgnl")
    public String testMybatisOgnl() {
        User user = new User();
        user.setId(1);
        user.setName("1");
        List<User> userList = userMapper.testMybatisOgnl(user);
        System.out.println(userList);
        return JSON.toJSONString(userList);
    }

    @BusinessLog(functionDesc = "测试切面日志功能")
    @GetMapping("/testLog")
    public String testLog(@RequestParam("param") String param) {
        System.out.println("执行业务逻辑");
        return "success";
    }

    @PostMapping("/testJemeter")
    public String testJemeter(@RequestBody AreaModel model) {
        return JSON.toJSONString(model);
    }

}

